package com.ruiyi.adrobot.mongo.company.impl;

import com.ruiyi.adrobot.domain.common.MongoPageRequest;
import com.ruiyi.adrobot.domain.company.Company;
import com.ruiyi.adrobot.mongo.company.CompanyOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by tao.zeng on 2017/8/2.
 */
public class CompanyRepositoryImpl implements CompanyOperations {
    @Autowired
    private MongoOperations mongo;

    @Override
    public Page<Company> getListByPage(MongoPageRequest<Map<String, Object>> mongoPageRequest) {
        Query query = new Query();
        Long count = mongo.count(query, Company.class);
        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "createDate"));
        Sort sort = new Sort(orders);
        mongoPageRequest.setSort(sort);
        List<Company> companys = mongo.find(query.with(mongoPageRequest), Company.class);
        return new PageImpl<Company>(companys, mongoPageRequest, count);
    }
}
